/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package snmpapplication.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import snmpapplication.Main;
import snmpapplication.log.Severity;

/**
 * Class used for ping action to a certain address
 * @author APetcu
 */
public class Ping {

    /**
     * Method used to ping @param ipAddress
     * @param ipAddress
     * @return 
     */
    public static boolean pingMe(String ipAddress) {

        try {
            InetAddress address = InetAddress.getByName(ipAddress);

            if (address.isReachable(2000)) {
                return true;
            }
            Main.log.log(new Severity(Severity.SEV_WaARN), "device " + ipAddress
                    + "not reachable by ping");
            return false;

        } catch (Exception e) {
            Main.log.log(new Severity(Severity.SEV_ERR), "Ping exception: " + e.getMessage());
            return false;
        }
    }

    public static String getMac(String ipAddress) {

        String s = "";
        try {
            InetAddress address = InetAddress.getByName("10.20.10.3");

            /*
            16.* Get NetworkInterface for the current host and then read the
            17.* hardware address.
            18.*/
            NetworkInterface ni = NetworkInterface.getByInetAddress(address);
            if (ni != null) {
                byte[] mac = ni.getHardwareAddress();
                if (mac != null) {
                    for (int i = 0; i < mac.length; i++) {
                        //s = String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");                        
                        
                        System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
                    }
                } else {
                    //System.out.println("Address doesn't exist or is not accessible.");
                    Main.log.log(new Severity(Severity.SEV_WaARN), "device " + ipAddress
                            + "not reachable by ping");
                }
            } else {
                //System.out.println("Network Interface for the specified address is not found.");
                Main.log.log(new Severity(Severity.SEV_WaARN), "Device with the specified address is not found");
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        
        return s;
    }
}
